package com.cindata

import com.cindata.dictionary.*
import grails.transaction.Transactional
import org.grails.plugins.excelimport.AbstractExcelImporter
import org.springframework.security.access.annotation.Secured

import java.text.DecimalFormat

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class EstateRealImportController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def evaluateService

    def sessionFactory

    def index(Integer max)
    {
    }

    @Transactional
    def excelImport()
    {
        /**
         * 读取文件*/
        def file = request.getFile('myFile')
        //将文件传给对应类,读取文件
        AbstractExcelImporter importer = new EstateExcelImporter()
        importer.read(file.getInputStream())

        def estateMapList = importer.getEstates()
        def estate;
        List<DataDict> dataList = DataDict.list();
        Map<String, DataDict> map = new HashMap<String, DataDict>();
        dataList.each {
            println(it.name + "  --  " + it.code)
            map.put(it.name, it)
        }

        estateMapList.each { Map param ->
            //遍历map集合
            param.estateStatus = param.estateStatus ? (EstateStatusDict) map.get(param.estateStatus) : null
            param.estateType = param.estateType ? (HouseTypeDict) map.get(param.estateType) : null
            param.isFirstHouse = (param.isFirstHouse == '是' ? true : false)
            param.starRepaymentMode = param.starRepaymentMode ? (RepaymentModeDict) map.get(param.starRepaymentMode) : null
            param.forwardLoanStep = param.forwardLoanStep ? (ForwardLoanStepDict) map.get(param.forwardLoanStep) : null
            param.loanMethod = param.loanMethod ? (LoanMethodDict) map.get(param.loanMethod) : null
            param.curRepaymentMode = param.curRepaymentMode ? (RepaymentModeDict) map.get(param.curRepaymentMode) : null
            param.guaranteeInstitution = param.guaranteeInstitution ? (Account) map.get(param.guaranteeInstitution) : null
            param.guaranteeMethod = param.guaranteeMethod ? (GuaranteeMethodDict) map.get(param.guaranteeMethod) : null
            param.borrAccountNature = param.borrAccountNature ? (AccountNatureDict) map.get(param.borrAccountNature) : null
            param.borrWorkUnitNature = param.borrWorkUnitNature ? (OrganizationNatureDict) map.get(param.borrWorkUnitNature) : null
            param.borrDepositedState = param.borrDepositedState ? (DepositedStateDict) map.get(param.borrDepositedState) : null
        }

        estateMapList.each {
            DecimalFormat df = new DecimalFormat("0");
            println(it.contractNo)
            def ss;
            try
            {
                ss = df.format(it.contractNo);
            }
            catch (Exception e)
            {
                ss = it.contractNo
            }
            println(ss)
            estate = Estate.findByContractNo(ss)
            if (!estate)
            {
                return
            }
            estate.mortgageeName = it.mortgageeName
            estate.estateType = it.estateType
            estate.accountName = it.accountName
            estate.isFirstHouse = it.isFirstHouse
            estate.downPaymentAmount = it.downPaymentAmount
            estate.publicFundLoansAmount = it.publicFundLoansAmount
            estate.bankLoansAmount = it.bankLoansAmount
            estate.loansBankName = it.loansBankName
            estate.starRepaymentMode = it.starRepaymentMode
            estate.repaymentDate = it.repaymentDate
            estate.forwardLoanStep = it.forwardLoanStep
            estate.loanMethod = it.loanMethod
            estate.curRepaymentMode = it.curRepaymentMode
            estate.curRecycleTotalCapital = it.curRecycleTotalCapital
            estate.curRecycleTotalInterest = it.curRecycleTotalInterest
            estate.curRecycleTotalFine = it.curRecycleTotalFine
            estate.curHasPaymentTODuration = it.curHasPaymentTODuration
            estate.curHasPaymentDuration = it.curHasPaymentDuration
            estate.guaranteeMethod = it.guaranteeMethod
            estate.guaranteeInstitution = it.guaranteeInstitution
            estate.borrPublicFundAccount = it.borrPublicFundAccount
            estate.borrName = it.borrName
            estate.borrPhone = it.borrPhone
            estate.borrIdCardNo = it.borrIdCardNo
            estate.borrBirthday = it.borrBirthday
            estate.borrAccountNature = it.borrAccountNature
            estate.borrOwnHouseCount = it.borrOwnHouseCount
            estate.borrWorkUnitName = it.borrWorkUnitName
            estate.borrWorkUnitNature = it.borrWorkUnitNature
            estate.borrDepositedState = it.borrDepositedState
            estate.borrDepositBaseOnLoans = it.borrDepositBaseOnLoans
            estate.borrDepositAmountOnLoans = it.borrDepositAmountOnLoans
            estate.borrDepositBaseOnCurrent = it.borrDepositBaseOnCurrent
            estate.borrDepositAmountOnCurrent = it.borrDepositAmountOnCurrent
            estate.borrFmiOnLoans = it.borrFmiOnLoans
            try
            {
                estate.save(flush: true)
            }
            catch (Exception ex)
            {
                println "error reason is ${ex.getMessage()}"
                return
            }
        }
        println("success")
        // evaluateService.batchInserts(estateMapList, newImportBatch, currentDate)
        // respond EstateRealImport, model: ["success": true], view: "message"
    }

}
